<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/19 0019
 * Time: 17:57
 */

namespace app\general\model;


use think\Exception;
use think\Model;

class SqArticle extends Model
{
    //每10分钟，增加一次阅读记录
    public function addArticleReaderNumber()
    {
        $this->startTrans();
        try{
            //获取文章add_reader_number不等于0的文章
            $arts = $this->field('id, reader_number, add_reader_number')->where('add_reader_number', 'neq', 0)->lock(true)->select();
            if(empty($arts)) {
                $this->commit();
                return 'nothing to do';
            }

            //获取机器人
            $sq_user_ids = (new SqUser())->where('is_robot',1)->column('id');
            $update_arts[] = [];  //sq_article
            $read_log_data[] = []; //sq_article_read_log
            $read_log_number = 0;  //记录插入sq_article_read_log数
            $read_log_level_data[] = []; //sq_article_read_log_level 记录一级
            $read_log_level_data2[] = []; //sq_article_read_log_level 记录一级
            $read_log_level_number = 0; //记录插入sq_article_read_log_level数
            $read_log_level_number2 = 0; //记录插入sq_article_read_log_level数
            $ids=array();
            $time = time();
            $number=1;
            foreach ($arts as $key=>$value) {
                //增加量，当机器人数量不足时，只插入机器人的数量
                $add_read_number = $value['add_reader_number'];
                if(count($sq_user_ids) < $add_read_number) $add_read_number = count($sq_user_ids);

                //sq_article
                $update_arts[$key] = [
                    'id' => $value['id'],
                    'reader_number' => $value['reader_number'] + $add_read_number,
                    'add_reader_number' => 0
                ];

                //插入的机器人用户
                $sq_users = array_rand($sq_user_ids,$add_read_number);
                //sq_article_read_log_level 层级中已记录该阅读的sq_user_id
                $user_ids = (new SqArticleReadLogLevel())->where('art_id',$value['id'])->column('user_id');
                $key=0;
                for($i=0; $i<$add_read_number; $i++){
                    $timer = rand(2,120);
                    $sq_user_id = $sq_user_ids[$sq_users[$i]];
                    //sq_article_read_log
                    $read_log_data[$read_log_number] = [
                        'user_id' => $sq_user_id,
                        'art_id' => $value['id'],
                        'create_time' => ceil($time - $timer/2),
                        'update_time' => ceil($time + $timer/2),
                        'time' => $timer,
                        'is_robot' => 1
                    ];
                    $read_log_number++;

                    //sq_article_read_log_level
                    if(!in_array($sq_user_id, $user_ids)){
                        if($key%2==0){
                            $read_log_level_data[$read_log_level_number] = [
                                'user_id' => $sq_user_id,
                                'art_id' => $value['id'],
                                'create_time' => $time,
                                'update_time' => $time,
                                'chain' => $sq_user_id,
                            ];
                            $ids[]=$sq_user_id;
                            $read_log_level_number++;
                            $key++;
                        }else{
                            $read_log_level_data2[$read_log_level_number2] = [
                                'user_id' => $sq_user_id,
                                'art_id' => $value['id'],
                                'level'=>1,
                                'create_time' => $time,
                                'update_time' => $time,
                                'chain' => $sq_user_id,
                            ];
                            $read_log_level_number2++;
                            if($number%7==0){
                               $key++;
                            }
                            $number++;
                        }
                    }
                }
            }
            //更新sq_artcile
            if(!$this->saveAll($update_arts)) throw new Exception('sq_artcile更新失败');
            //插入sq_article_read_log
            if(!(new SqArticleReadLog())->saveAll($read_log_data)) throw new Exception('sq_article_read_log插入失败');
            //插入sq_article_read_log_level
            if(!empty($read_log_level_data)){
                if(!(new SqArticleReadLogLevel())->saveAll($read_log_level_data)) throw new Exception('sq_article_read_log_level插入失败');
            }
            if(!empty($read_log_level_data2)){
                $fromdata=array();
                $level=0;
                foreach ($read_log_level_data2 as $k=>&$value){
                    if($k%7==0&&$k!=0){
                       $level++;
                    }
                    $chain=$ids[$level]."-".$value['chain'];
                    $value['chain']=$chain;
                    $value['parent_id']=$ids[$level];
                    $fromdata[]=$value;
                }
                if(!(new SqArticleReadLogLevel())->saveAll($fromdata)) throw new Exception('sq_article_read_log_level插入失败');
            }

            $this->commit();
        }catch (Exception $exception){
            $this->rollback();
            $exception->getMessage();
        }
        return 'success';
    }


}